﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XStudio.WebFramework.Mvc.Filter;
using XStudio.WebFramework.Support;
using XStudio.Core.Ext.ConvertExtensions;
using XStudio.Framework.Extensions;
using System.ComponentModel;
using Core.Manage.User;

namespace Admin.Areas.User.Controllers
{
    [AuthorizeFilter]
    [Description("农户管理")]
    public class FamerController : Controller
    {
        #region 添加农户
        /// <summary>
        /// 添加农户
        /// </summary>
        /// <returns></returns>
        [Description("添加农户")]
        public ActionResult Create()
        {
            var entity = new FamerVWEntity();

            ViewData["cooID"] = GetCoo(-1);
            return View(entity);
        }

        /// <summary>
        /// 提交添加IMEI数据
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(int cooID, int sexID, FamerVWEntity entity, FormCollection collection)
        {
            var result = AccountRule.Instance.Insert(0, entity.Account, entity.Password);

            if (!result.IsOK)
                return Json(result);

            result = FamerRule.Instance.Insert(result.StateCodeID, cooID, entity.NickName, sexID, entity.PhoneNo, entity.FamilyAddress);

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 添加合作社
        /// <summary>
        /// 添加合作社
        /// </summary>
        /// <returns></returns>
        [Description("添加合作社")]
        public ActionResult Edit(int id)
        {
            var entity = FamerVWRule.Instance.GetEntity(id);
            ViewData["cooID"] = GetCoo(entity.CooperativeID);
            return View("Create", entity);
        }

        /// <summary>
        /// 提交添加IMEI数据
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, int cooID, int sexID, FamerVWEntity entity, FormCollection collection)
        {
            AccountRule.Instance.Update(id, entity.Password);
            var result = FamerRule.Instance.Update(id, cooID, entity.NickName, sexID, entity.PhoneNo, entity.FamilyAddress);

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 获取合作社列表
        /// <summary>
        /// 获取合作社列表
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(true)]
        [Description("获取合作社列表")]
        public ActionResult List(int id)
        {
            ViewBag.ID = id;
            return View();
        }

        /// <summary>
        /// 按条件查询列表信息
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(true)]
        [HttpPost]
        public ActionResult ListData(int id)
        {
            int pageNo = Request["page"].ToInt32();
            if (pageNo < 1)
                pageNo = 1;
            int pageSize = Request["rows"].ToInt32();
            if (pageSize < 10)
                pageSize = 30;
            var searchCondition = " 1=1 ";

            if (id != 0)
            {
                searchCondition += " and CooperativeID=" + id;
            }

            //搜索条件
            if (!string.IsNullOrWhiteSpace(Request["SearchWord"]))
            {
                var searchWord = Request["SearchWord"].LikeFilter();
                searchCondition += string.Format(" AND (IMEI LIKE '%{0}%')", searchWord);
            }

            var data = FamerVWRule.Instance.GetGridDataByPage(pageSize, pageNo - 1, true, searchCondition);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 获取合作社
        public List<SelectListItem> GetCoo(int selectID)
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Value = "-1", Text = "请选择合作社" });
            var data = CooperativeRule.Instance.GetEntityList(" 1=1");

            foreach (var item in data)
            {
                var items = new SelectListItem() { Value = item.UID.ToString(), Text = item.Name };

                if (item.UID == selectID)
                    items.Selected = true;

                list.Add(items);
            }

            return list;
        }
        #endregion

        #region 农户地址
        /// <summary>
        /// 农户地址
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(true)]
        [Description("农户地址")]
        public ActionResult AddressList(int id)
        {
            ViewBag.ID = id;
            return View();
        }

        /// <summary>
        /// 按条件查询列表信息
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(true)]
        [HttpPost]
        public ActionResult AddressListData(int id)
        {
            int pageNo = Request["page"].ToInt32();
            if (pageNo < 1)
                pageNo = 1;
            int pageSize = Request["rows"].ToInt32();
            if (pageSize < 10)
                pageSize = 30;
            var searchCondition = " 1=1 ";

            searchCondition += " and FamerID=" + id;

            //搜索条件
            if (!string.IsNullOrWhiteSpace(Request["SearchWord"]))
            {
                var searchWord = Request["SearchWord"].LikeFilter();
                searchCondition += string.Format(" AND (IMEI LIKE '%{0}%')", searchWord);
            }

            var data = AddressRule.Instance.GetGridDataByPage(pageSize, pageNo - 1, true, searchCondition);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}